<% layout('/layouts/default.html', {title: '药材收获信息管理', libs: ['validate', 'dataGrid']}){ %>
<div class="main-content">
	<div class="box box-main">
		<div class="box-header with-border">
			<div class="box-title">
				<i class="fa icon-note"></i> ${text(ownMedicineReceiptInformation.isNewRecord ? '新增药材收获信息' : '编辑药材收获信息')}
			</div>
			<div class="box-tools pull-right">
				<button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>
			</div>
		</div>
		<#form:form id="inputForm" model="${ownMedicineReceiptInformation}" action="${ctx}/own/ownMedicineReceiptInformation/save" method="post" class="form-horizontal">
			<div class="box-body">
				<div class="form-unit">${text('基本信息')}</div>
				<div class="row" id="info" hidden>
					<div class="col-xs-4">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">${text('种子种苗植物名')}：</label>
							<label class="col-sm-8" id="seednm"></label>
						</div>
					</div>
					<div class="col-xs-4">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">${text('种植批次码')}：</label>
							<label class="col-sm-8" id="plantpc"></label>
						</div>
					</div>
					<div class="col-xs-4">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">${text('种植时间')}：</label>
							<label class="col-sm-8" id="planttt"></label>
						</div>
					</div>
				</div>
				<#form:hidden path="id"/>
				<#form:hidden id="ownMedicinePlantInformationId" path="ownMedicinePlantInformationId"/>
				<div class="row">
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required ">*</span> ${text('种植批次号')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:listselect id="batchId" title="种植批次号" labelPath="batchId"
								url="${ctx}/own/ownMedicinePlantInformation/findByKey" allowClear="false" checkbox="false"
								itemCode="batchId" itemName="batchId" class="form-control required"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required ">*</span> ${text('药材批次号')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="medicineBatchId" maxlength="25" class="form-control required"/>
							</div>
						</div>
					</div>
				</div>
				<div class="row">
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required ">*</span> ${text('药材名称')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input id="varietyName" path="varietyName" maxlength="50" class="form-control required"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required">*</span> ${text('药材重量(g)')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="weight" class="form-control number required"/>
							</div>
						</div>
					</div>
				</div>
				<div class="row">
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required ">*</span> ${text('所属区域代码')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input id="areaCode" path="areaCode" maxlength="6" class="form-control required"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required ">*</span> ${text('收获时间')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="harvestTime" readonly="true" maxlength="20" class="form-control Wdate required"
									dataFormat="datetime" onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm',isShowClear:true});"/>
							</div>
						</div>
					</div>
				</div>
				<div class="row">
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required ">*</span> ${text('种子来源')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:radio path="seedSource" dictType="own_medicine_seed_source" class="form-control required" />
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required ">*</span> ${text('施肥情况')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:radio path="fertilizer" dictType="own_medicine_fertilizer" class="form-control required" />
							</div>
						</div>
					</div>
				</div>
				<div class="row">
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required ">*</span> ${text('喷药情况')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:radio path="pesticide" dictType="own_medicine_pesticide" class="form-control required" />
							</div>
						</div>
					</div>
					<% if (ownMedicineReceiptInformation.isNewRecord){ %>
						<div class="col-xs-6">
							<div class="form-group">
								<label class="control-label col-sm-4" title="">
									<span class="required hide">*</span> ${text('更新种植信息的状态为收获完成')}：<i class="fa icon-question hide"></i></label>
								<div class="col-sm-8">
									<#form:radio name="finished" dictType="sys_yes_no" class="form-control" />
								</div>
							</div>
						</div>
					<% } %>
				</div>
				<div class="row">
					<div class="col-xs-12">
						<div class="form-group">
							<label class="control-label col-sm-2" title="">
								<span class="required hide">*</span> ${text('备注')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-10">
								<#form:textarea path="remarks" rows="4" maxlength="255" class="form-control"/>
							</div>
						</div>
					</div>
				</div>
				<!--<h4 class="form-unit">添加检验信息</h4>
				<div class="ml10 mr10">
					<table id="inspectionChildDataGrid"></table>
					<% if (false){ %>
					<a id="inspectionChildDataGridAddRowBtn" class="btn btn-primary btn-sm mt10 mb10" ><i class="fa fa-plus"></i> 增行</a>
					<% } %>
				</div>-->
			</div>
			<div class="box-footer">
				<div class="row">
					<div class="col-sm-offset-2 col-sm-10">
						<% if (hasPermi('own:ownMedicineReceiptInformation:edit')){ %>
							<button type="submit" class="btn btn-sm btn-primary" id="btnSubmit"><i class="fa fa-check"></i> ${text('保 存')}</button>&nbsp;
						<% } %>
						<button type="button" class="btn btn-sm btn-default" id="btnCancel" onclick="js.closeCurrentTabPage()"><i class="fa fa-reply-all"></i> ${text('关 闭')}</button>
					</div>
				</div>
			</div>
		</#form:form>
	</div>
</div>
<% } %>
<!--<script>
	$.ajax({
		url: "${ctx}/own/ownMedicineInspectionInformation/findByReceiptId",
		type: "get",
		data: {"receiptId": "${ownMedicineReceiptInformation.id}"},
		dataType: "json",
		success: function (data) {
			var showdata = [];
			if (data.result){
				showdata = data.data?data.data:[];
				//初始化检验信息子表DataGrid对象
				$("#inspectionChildDataGrid").dataGrid({
					data: showdata,
					datatype: "local", // 设置本地数据
					autoGridHeight: function(){return 'auto'}, // 设置自动高度

					// 设置数据表格列
					columnModel: [
						{header:'主键', name:'id', editable:true, hidden:true},
						{header:'检验信息ID', name:'ownMedicineReceiptInformationId', editable:true, hidden:true},
						{header:'检验方法', name:'checkMethod', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'50', 'class':'form-control', 'rows':'1'}},
						{header:'质量级别', name:'checkGrade', width:100,
							editable:true, edittype:'select', editoptions:{'class':'form-control',
								items: $.merge([{dictLabel:'&nbsp;',dictValue:''}], ${@DictUtils.getDictListJson('own_medicine_check_grade')}),
								itemLabel: 'dictLabel', itemValue: 'dictValue', dataInit: function(element){
									$(element).select2().on("change",function(){$(this).resetValid()});
								}
							}
						},
						{header:'粗加工方法', name:'checkThickMethod', width:100,
							editable:true, edittype:'select', editoptions:{multiple:true, 'class':'form-control',
								items: $.merge([], ${@DictUtils.getDictListJson('own_medicine_thick_method')}),
								itemLabel: 'dictLabel', itemValue: 'dictValue', dataInit: function(element){
									$(element).select2().on("change",function(){$(this).resetValid()});
								}
							}
						},
						{header:'贮藏条件', name:'checkCondition', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'50', 'class':'form-control', 'rows':'1'}},
						{header:'检验人员', name:'checkUser', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'50', 'class':'form-control', 'rows':'1'}},
						{header:'检验时间', name:'checkTime', width:150,
							formatter:'date', formatoptions:{srcformat:'Y-m-d H:i:s',newformat:'Y-m-d H:i:s'},
							editable:true, edittype:'text', editoptions:{'class':'form-control Wdate ', 'readonly':'true',
								dataInit: function(element){ $(element).on('focus', function(){
									WdatePicker({dateFmt:'yyyy-MM-dd HH:mm',isShowClear:false});
								});
								}}
						},
						{header:'药材剩余重量(g)', name:'residualWeight', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'25', 'class':'form-control number', 'rows':'1'}},
						{header:'备注', name:'remarks', width:150, editable:true, edittype:'textarea', editoptions:{'maxlength':'255', 'class':'form-control number', 'rows':'2'}},
						{header:'操作', name:'actions', width:80, sortable:false, fixed:true, formatter: function(val, obj, row, act){
								var actions = [];
								if (val == 'new'){
									actions.push('<a href="#" onclick="js.confirm(\'你确认要删除这条数据吗？\', function(){$(\'#growerChildDataGrid\').dataGrid(\'delRowData\',\''+obj.rowId+'\')});return false;"><i class="fa fa-trash-o"></i></a>&nbsp;');
								}
								return actions.join('');
							}, editoptions: {defaultValue: 'new'}}
					],

					// 编辑表格参数
					editGrid: true,				// 是否是编辑表格
					editGridInitRowNum: 0,		// 编辑表格的初始化新增行数
					editGridAddRowBtn: $('#inspectionChildDataGridAddRowBtn'),	// 子表增行按钮
					editGridAddRowInitData: {},	// 新增行的时候初始化的数据

					// 编辑表格的提交数据参数
					editGridInputFormListName: '', // 提交的数据列表名
					editGridInputFormListAttrs: '', // 提交数据列表的属性字段

					// 加载成功后执行事件
					ajaxSuccess: function(data){

					}
				});
			}else {
				js.showErrorMessage(`获取错误！`+data.message);
			}
		},
		error: function(e){
			js.showErrorMessage("错误",e);
		}
	});

</script>
<script id="listselectTpl" type="text/template">//&lt;!&ndash;<div>
<#form:listselect id="{{d.id}}" title="{{d.title}}" name="{{d.name}}" value="{{d.value}}"
	labelName="{{d.labelName}}" labelValue="{{d.labelValue}}" url="{{d.url}}"
	itemCode="{{d.itemCode}}" itemName="{{d.itemName}}"
	class="{{d.cssClass}}" btnClass="btn-sm" allowClear="true"/>
</div>//&ndash;&gt;</script>-->
<script>
$("#inputForm").validate({
	submitHandler: function(form){
		if ($("input[name='finished']:checked").val() == 1){
			js.confirm("确认要将该种植信息更新收获完成，并且不能再进行更改？",
					"${ctx}/own/ownMedicinePlantInformation/updateStatus",
					{"plantId": $('#ownMedicinePlantInformationId').val(), "status": "2"},
					function(data){
						if (data.result){
							js.ajaxSubmitForm($(form), function(data){
								js.showMessage(data.message);
								if(data.result == Global.TRUE){
									js.closeCurrentTabPage(function(contentWindow){
										contentWindow.page();
									});
								}
							}, "json");
						}else {
							js.showErrorMessage("收获失败，错误信息"+data.message+"!");
						}
					}, "json", false, "收获成功！");
		}else {
			doit(form, '1');
		}

    }
});
function doit(form, status){
	$.ajax({
		url: "${ctx}/own/ownMedicinePlantInformation/updateStatus",
		type: "get",
		data: {"plantId": $('#ownMedicinePlantInformationId').val(), "status": status},
		dataType: "json",
		success: function(data){
			if (!data.result){
				js.showErrorMessage("获取失败，错误信息"+data.message+"!");
			}else {
				js.ajaxSubmitForm($(form), function(data){
					js.showMessage(data.message);
					if(data.result == Global.TRUE){
						js.closeCurrentTabPage(function(contentWindow){
							contentWindow.page();
						});
					}
				}, "json");
			}
		},
		error: function(e){
			js.showErrorMessage("错误",e);
		}
	})

}
//选择种植批次号回调方法
function  listselectCallback(id, act, index, layero, selectData) {
	if (id == 'batchId' && (act == 'ok' || act == 'clear')){
		for (let key in selectData){
			$('#ownMedicinePlantInformationId').val(selectData[key]['id']);
			$('#varietyName').val(selectData[key]['varietyName']);
			$('#areaCode').val(selectData[key]['areaCode']);
			$("input[name='seedSource'][value='"+selectData[key]['seedSource']+"']").parent().click();
			$("#seednm").text(selectData[key].varietyName);
			$("#plantpc").text(selectData[key].bmBatchCode);
			$("#planttt").text(selectData[key].plantTime);
			$("#info").show();
			/*$("input[name='seedSource']").parent().removeClass("checked").attr("aria-checked","false");
			$("input[name='seedSource'][value='"+selectData[key]['seedSource']+"']").parent().addClass("checked").attr("aria-checked","true");*/
			// $('#seedSource').val(selectData[key]['seedSource']);
			$.ajax({
				url: "${ctx}/own/ownMedicineFieldManagement/findByType",
				type: "get",
				data: {"plantId": selectData[key]['id']},
				dataType: "json",
				success: function(data){
					if (data.success){
						if (data.sf){
							//喷药
							$("input[name='fertilizer'][value='已施肥']").parent().click();
						}else {
							$("input[name='fertilizer'][value='未施肥']").parent().click();
						}
						if(data.py){
							//施肥
							$("input[name='pesticide'][value='已喷药']").parent().click();
						}else {
							$("input[name='pesticide'][value='未喷药']").parent().click();
						}
					}else {
						js.showErrorMessage("获取失败，错误信息"+data.msg+"!");
					}
				},
				error: function(e){
					js.showErrorMessage("错误",e);
				}

			})
			break;
		}
	}
}
$(function(){
	var jsaa = "${ownMedicineReceiptInformation.isNewRecord}"; //获取beetl的值
	if (jsaa == 'false'){
		$("#batchIdName").attr("disabled","disabled");
		$("#info").show();
	}

	$.ajax({
		url: "${ctx}/own/ownMedicinePlantInformation/findById",
		type: "get",
		data: {"id": "${ownMedicineReceiptInformation.ownMedicinePlantInformationId}"},
		dataType: "json",
		success: function (data) {
			if (data.ownMedicinePlantInformation){
				$("#seednm").text(data.ownMedicinePlantInformation.varietyName);
				$("#plantpc").text(data.ownMedicinePlantInformation.bmBatchCode);
				$("#planttt").text(data.ownMedicinePlantInformation.plantTime);
			}else {
				js.showErrorMessage("获取错误！", data.msg);
			}
		},
		error: function (e) {
			js.showErrorMessage("错误", e);
		}
	});
	$("input[name='residualWeight']").attr("readonly","readonly");
	$("#weight").on("input", function () {
		$("input[name='residualWeight']").val($("#weight").val());
	});
});
</script>